import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import static java.nio.file.StandardCopyOption.*;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


public class DisplayPanel extends JPanel{

	/**
	 * Default Serialization
	 */
	private static final long serialVersionUID = 1L;
	
	private JScrollPane scroll;
	private JPanel picturePanel;
	private File currentPic;
	
	public DisplayPanel(){
		super(new BorderLayout());
		JPanel panel = new JPanel(new GridBagLayout());
		picturePanel = new JPanel(new BorderLayout());
		scroll = new JScrollPane(picturePanel);
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.weightx = 1;
		gbc.weighty = 1;
		panel.add(scroll, gbc);
		
		JPanel buttonPanel = new JPanel(new GridBagLayout());
		JButton build = new JButton("Build Call Graph");
		JPanel space = new JPanel();
		gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.weightx = 1;
		buttonPanel.add(space, gbc);
		gbc = new GridBagConstraints();
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.insets = new Insets(5, 5, 5, 5);
		buttonPanel.add(build, gbc);
		JButton save = new JButton("Save");
		gbc = new GridBagConstraints();
		gbc.gridx = 2;
		gbc.gridy = 0;
		gbc.insets = new Insets(5, 5, 5, 5);
		buttonPanel.add(save, gbc);
		
		gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.weightx = 1;
		gbc.weighty = 0;
		panel.add(buttonPanel, gbc);
		add(panel, BorderLayout.CENTER);
		
		build.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				File jlex = new File("c:\\Users\\Jamison\\workspace\\JLex.jar");
				File clientEmail = new File("c:\\Users\\Jamison\\workspace\\ClientEmail\\src");
				JFileChooser fc = new JFileChooser();
				fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
				int val = fc.showOpenDialog(scroll);
				if (val == JFileChooser.APPROVE_OPTION){
					if (fc.getSelectedFile().equals(jlex)){
						cursorWait();
						addPicture(new File("c:\\Users\\Jamison\\Documents\\SE416\\JLex.png"));
						PrototypeGui.side.addProject(jlex.getPath().toString());
					}
					else if (fc.getSelectedFile().equals(clientEmail)){
						File output = new File("c:\\Users\\Jamison\\Documents\\SE416\\output.txt");
						PrototypeGui.side.addProject(clientEmail.getPath().toString());
						try{
							BufferedReader reader = new BufferedReader(new FileReader(output));
							StringBuilder builder = new StringBuilder(20000);
							String line = null;
							while ((line = reader.readLine()) != null){
								builder.append(line);
								builder.append("\n");
							}
							PrototypeGui.console.printText(builder.toString());
						} 
						catch (FileNotFoundException e){
							e.printStackTrace();
						} 
						catch (IOException e){
							e.printStackTrace();
						}
						cursorWait();
						addPicture(new File("c:\\Users\\Jamison\\Documents\\SE416\\ClientEmail.png"));
					}
				}
			}
		});
		
		save.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (currentPic != null){
					JFileChooser fc = new JFileChooser();
					int val = fc.showOpenDialog(PrototypeGui.frame);
					if (val == JFileChooser.APPROVE_OPTION){
						try{
							InputStream in = new FileInputStream(currentPic);
							OutputStream out = new FileOutputStream(fc.getSelectedFile());
							byte[] buf = new byte[1024];
							int len;
							while ((len = in.read(buf)) > 0){
								out.write(buf, 0, len);
							}
							in.close();
							out.close();
						} 
						catch (FileNotFoundException e){
							e.printStackTrace();
						} 
						catch (IOException e){
							e.printStackTrace();
						}
						
					}
				}
				else{
					JOptionPane.showMessageDialog(PrototypeGui.frame, "No graph is currently being displayed");
				}
			}
		});
	}
	
	public void addPicture(File f){
		currentPic = f;
		picturePanel.removeAll();
		try{
			BufferedImage image = ImageIO.read(f);
			JLabel picture = new JLabel(new ImageIcon(image));
			picturePanel.add(picture, BorderLayout.CENTER);
			scroll.revalidate();
			scroll.repaint();
		} 
		catch (IOException e){
			e.printStackTrace();
		}
	}
	
	public static void cursorWait(){
		Cursor cursor = new Cursor(Cursor.WAIT_CURSOR);
		PrototypeGui.frame.setCursor(cursor);
		long start = System.currentTimeMillis();
		while (System.currentTimeMillis() - start < 2000);
		PrototypeGui.frame.setCursor(Cursor.getDefaultCursor());
	}
	
	
}
